package com.cnsoft.portal.home;

import java.util.List;

import org.cnsoft.framework.beans.MyBeanFactoryHelper;
import org.cnsoft.framework.beans.common.RESTResultBean;
import org.cnsoft.framework.beans.user.UserBean;
import org.cnsoft.framework.cache.session.SessionHelper;
import org.cnsoft.framework.json.JSONObject;
import org.cnsoft.framework.support.MyServiceSupport;
import org.cnsoft.persistent.common.ManagerMenu.ManagerMenuDBO;
import org.cnsoft.persistent.common.ManagerMenu.ManagerMenuDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 超管菜单权限
 * 
 * @author ZmSoft
 * @version 0.1.0 2018/3/1
 * @since 0.1.0 2018/3/1
 */
@Service("ISManagerHomeService")
// @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class ManagerHomeService extends MyServiceSupport implements ISManagerHomeService {

	public RESTResultBean<UserBean> doUserInfo() {
		RESTResultBean<UserBean> result = new RESTResultBean<UserBean>();
		result.setData(SessionHelper.currentUser());
		return result;
	}
	
	@Transactional(rollbackFor = { Exception.class, RuntimeException.class })
	public RESTResultBean<List<ManagerMenuDBO>> doUserMenu(ManagerMenuDBO param) throws Exception {
		ManagerMenuDao _ManagerMenuService_ = MyBeanFactoryHelper.getBean(ManagerMenuDao.class);
//		ManagerMenuDBO param = new ManagerMenuDBO();
		UserBean _UserBean_ = SessionHelper.currentUser();
		RESTResultBean<List<ManagerMenuDBO>> result = new RESTResultBean<List<ManagerMenuDBO>>();

		// 用户角色判断
		List<ManagerMenuDBO> menuDatas;
		if (ONE.equals(_UserBean_.getIsAdmin())) {
//			_ManagerMenuDBO_.setFatherMenuId(param.getFatherMenuId());
			param.setMenuLevel(ONE);
			menuDatas = _ManagerMenuService_.doSelectData(param);// 如果管理员则查询全部菜单
			// 获取一级菜单
			for (ManagerMenuDBO menu : menuDatas) {
				param.setMenuLevel(TWO);
				param.setFatherMenuId(menu.getId());
				menu.setNextSubs(_ManagerMenuService_.doSelectData(param));
			}
		} else {
//			_ManagerMenuDBO_.setFatherMenuId(param.getFatherMenuId());
			// 权限查询参数
			param.setUserId(SessionHelper.currentUserId());
			// 获取一级菜单
			param.setMenuLevel(ONE);

			{
				// 获取一级菜单
				menuDatas = _ManagerMenuService_.doSelectMenusByPermission(param);// 如果管理员则查询全部菜单
				// 获取二级菜单
				for (ManagerMenuDBO menu : menuDatas) {
					param.setMenuLevel(TWO);
					param.setFatherMenuId(menu.getId());
					menu.setNextSubs(_ManagerMenuService_.doSelectMenusByPermission(param));
				}
			}
		}

		result.setData(menuDatas);

		logger.debug("RESTResultBean==>>>" + JSONObject.toJsonString(result.getData()));
		return result;
	}

	public RESTResultBean<List<ManagerMenuDBO>> doUserRole(ManagerMenuDBO param) throws Exception {
		return null;
	}

}
